<?php
// is the login type a correct value? 
if(isset($parameters[1]) && in_array($parameters[1], array('leerlingen', 'docenten')))
{
	// not all required fields entered
	if(empty($_POST['gebruikersnaam']) || empty($_POST['wachtwoord']))
	{
		header("Location: " . path . "loginerror");
		exit;
	}
	else
	{
		$password = hash('md5', $_POST['wachtwoord']);

		// in case the login is klassetv
		if(isset($_POST['klassetv']) && $_POST['klassetv'] == 'admin')
		{
			// if the login is correct create session and redirect, if not redirect to loginerror page
			if($_POST['gebruikersnaam'] == admin_user && $password == admin_password)
			{
				$_SESSION['userSession'] = '3:null';
				header("Location: " . path . "index");
			}
			else
			{
				header("Location: " . path . "loginerror");
			}
			exit;
		}

		// check if the user exists
		$userExists = userHandler::getUser($parameters[1], $_POST['gebruikersnaam'], $password);

		// if the user exists, log the user in and redirect, if not redirect to loginerror page
		if($userExists)
		{
			userHandler::logIn($parameters[1], $userExists['id']);
			header("Location: " . path . "index");
		}
		else
		{
			header("Location: " . path . "loginerror");
			exit;
		}
	}
}
else
{
	header("Location: " . path . "loginerror");
	exit;
}